package shared.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import server.util.Logger;
import client.gui.ErrorMessageGui;

/**
 * The Class Config
 * used to read data from the configuration file.
 */
public class Config {
	
	/** The configration file. */
	File file;
	
	/** The servers ip. */
	String ip;
	
	/** The port to use when connecting to the server. */
	int port;
	
	/** The size of one block. */
	int size;
	
	/** Time to wait between moves (basically the speed of the game) */
	int waitBeforeMove;
	
	/** Time to wait before the game starts */
	int waitBeforeReady;
	
	/** The string used for parsing the xml file. */
	String helpString;
	
	/** The logger for logging errors. */
	Logger logger = null;
	
	/**
	 * Instantiates a new config.
	 *
	 * @param fileLocation the configuration file location
	 * @param logger 
	 */
	public Config(String fileLocation, Logger logger) throws FileNotFoundException{
		file = new File(fileLocation);
		this.logger = logger;
		getConfig();
	}
	
	/**
	 * Instantiates a new config.
	 *
	 * @param fileLocation the configuration file location
	 */
	public Config(String fileLocation) throws FileNotFoundException{
		file = new File(fileLocation);
		getConfig();
	}

	/**
	 * Gets the configuration file data.
	 *
	 * @throws FileNotFoundException the file not found exception
	 */
	private void getConfig() throws FileNotFoundException{
		FileReader fstream = new FileReader(file);
		BufferedReader br = new BufferedReader(fstream);
		//start the infinite loop
		while (true) {
			String line = null;
			try {
				line = br.readLine();
			} catch (IOException e) {
				if(logger != null){
					logger.logError("Config readLine() error");
				}else{
					new ErrorMessageGui("Config readLine() error");
				}
			}
			if (line != null) {
				helpString = "ip";
				if(line.indexOf(helpString) != -1){
					ip =  line.substring(line.indexOf(helpString) + helpString.length() + 1, line.length() - (helpString.length() + 3));
				}
				helpString = "port";
				if(line.indexOf(helpString) != -1){
					port =  Integer.parseInt(line.substring(line.indexOf(helpString) + helpString.length() + 1, line.length() - (helpString.length() + 3)));
				}
				helpString = "size";
				if(line.indexOf(helpString) != -1){
					size =  Integer.parseInt(line.substring(line.indexOf(helpString) + helpString.length() + 1, line.length() - (helpString.length() + 3)));
				}
				helpString = "waitBeforeMove";
				if(line.indexOf(helpString) != -1){
					waitBeforeMove =  Integer.parseInt(line.substring(line.indexOf(helpString) + helpString.length() + 1, line.length() - (helpString.length() + 3)));
				}
				helpString = "waitBeforeReady";
				if(line.indexOf(helpString) != -1){
					waitBeforeReady =  Integer.parseInt(line.substring(line.indexOf(helpString) + helpString.length() + 1, line.length() - (helpString.length() + 3)));
				}
			}else{
				try {
					fstream.close();
				} catch (IOException e) {
					if(logger != null){
						logger.logError("Could not close fstream");
					}else{
						new ErrorMessageGui("Could not close fstream");
					}
				}
				break;
			}
		}
	}
	
	/**
	 * Gets the servers ip from the previously loaded xml file.
	 *
	 * @return the ip
	 */
	public String getIp(){
		return ip;
	}
	
	/**
	 * Gets the servers port from the previously loaded xml file.
	 *
	 * @return the port number
	 */
	public int getPort(){
		return port;
	}
	
	/**
	 * Gets the size of the block from the previously loaded xml file.
	 *
	 * @return the size
	 */
	public int getSize(){
		return size;
	}
	
	/**
	 * Gets the time needed to wait before starting another move from the previously loaded xml file.
	 *
	 * @return the amount of time
	 */
	public int getWaitBeforeMove(){
		return waitBeforeMove;
	}
	
	/**
	 * Gets the time needed to wait at the start of the game.
	 *
	 * @return the time needed to wait
	 */
	public int getWaitBeforeReady(){
		return waitBeforeReady;
	}
}
